import Vue from 'vue'

import QuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.bubble.css'
import 'quill/dist/quill.snow.css'

Vue.use(QuillEditor)
import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import locale from 'element-ui/lib/locale/lang/zh-CN' // lang i18n

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'
import '@/icons' // icon
import '@/permission' // permission control
// 将后台返回的时间戳转换成日期格式
// import moment from 'moment'
/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */
// import { mockXHR } from '../mock'
import qs from 'qs'
import axios from 'axios'
Vue.prototype.$axios = axios
Vue.prototype.$qs = qs
axios.interceptors.request.use(
  config => {
    // 这里写死一个token，你需要在这里取到你设置好的token的值
    const token = 'this is a token'
    if (token) {
      // 这里将token设置到headers中，header的key是Authentication，这个key值根据你的需要进行修改即可
      config.headers.Authentication = store.getters.token
    }
    if (config.method === 'post') {
      config.data = qs.stringify(config.data)
      // config.headers.contentType = "application/x-www-form-urlencoded"
    }
    return config
  },
  error => {
    return Promise.reject(error)
  })
import { FromDates } from './components/util/util'
Vue.prototype.$fromdate = FromDates
//  在原型链中注册，然后再需要引用的页面就可以这样拿到--调用方法：：this.$fromdate()
Vue.prototype.$http = axios

if (process.env.NODE_ENV === 'production') {
  // mockXHR()
}
// set ElementUI lang to EN
Vue.use(ElementUI, { locale })

Vue.config.productionTip = false

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})

// main.js

router.beforeEach((to, from, next) => {
  console.log('router.beforeEach')
  if (store.getters.token) { // 判断是否有token
    console.log('判断有token')
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      console.log('判断有token')
      if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
        store.dispatch('GetInfo').then(res => { // 拉取info
          const roles = res.data.role
          console.log(roles)
          store.dispatch('GenerateRoutes', { roles }).then(() => { // 生成可访问的路由表
            router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
            next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
          })
        }).catch(err => {
          console.log(err)
        })
      } else {
        next() // 当有用户权限的时候，说明所有可访问路由已生成 如访问没权限的全面会自动进入404页面
      }
    }
  } else {
    console.log('判断没有token')
    const whiteList = ['/login'] // no redirect whitelist
    if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单，直接进入
      next()
    } else {
      next('/login') // 否则全部重定向到登录页
    }
  }
})
